package edu.nf.ch07.service;

import com.github.pagehelper.PageInfo;
import edu.nf.ch07.entity.City;
import edu.nf.ch07.mapper.CityMapper;
import edu.nf.ch07.util.MybatisUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author CSY
 * @date 2025 - 04 - 25
 */
public class CityService {
    public List<City> pageCity(int pageNum, int pageSize) {
        //将分页参数封装成RowBounds对象
        RowBounds rowBounds = new RowBounds(pageNum, pageSize);
        try(SqlSession session = MybatisUtils.getSqlSession()) {
            return session.getMapper(CityMapper.class).pageCity(rowBounds);
        }
    }

    public PageInfo<City> pageCity2(int pageNum, int pageSize) {
        try(SqlSession session = MybatisUtils.getSqlSession()) {
            //list实际上是一个代理对象
            List<City> list = session.getMapper(CityMapper.class).pageCity2(pageNum, pageSize);
            //将分页记录封装成分页对象(总记录数、总页数、起始页)
            return new PageInfo<>(list);
        }
    }
}
